package com.fart.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fart.common.R;
import com.fart.domain.Category;
import com.fart.domain.Dish;
import com.fart.domain.DishDto;
import com.fart.service.CategoryService;
import com.fart.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        //测试
        //log.info("dishDto:{}",dishDto);

        dishService.saveDishDto(dishDto);
        return R.success("添加菜品成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        //1.构造分页构造器对象
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        //6.构建分页对象 -> 为了展示categoryName
        Page<DishDto> dishPageInfo = new Page<>(page,pageSize);
        //2.条件构造器
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //3.添加过滤条件
        lqw.like(name != null,Dish::getName,name);
        //4.添加排序条件
        lqw.orderByDesc(Dish::getUpdateTime);
        //5.执行分页查询
        dishService.page(pageInfo,lqw);
        //7.进行对象拷贝,将pageInfo信息 拷贝到 dishPageInfo 但是除了records
        BeanUtils.copyProperties(pageInfo,dishPageInfo,"records");
        //7.1手动封装records,获取pageInfo中的records
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> collect = records.stream().map((list) -> {
            //7.2遍历菜品list,获取所有的菜品
            DishDto dishDto = new DishDto();
            //拷贝
            BeanUtils.copyProperties(list, dishDto);
            //获取菜品id
            Long id = list.getCategoryId();
            //根据菜品id获取 菜品名称 -> category表中
            Category category = categoryService.getById(id);
            if (category != null) {
                //获取菜品名称
                String categoryName = category.getName();
                //设置tto对象中的CategoryName
                dishDto.setCategoryName(categoryName);
            }
            //将dishDto对象返回
            return dishDto;
        }).collect(Collectors.toList());

        //将records设置到dishPageInfo
        dishPageInfo.setRecords(collect);
        return R.success(dishPageInfo);
    }
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish){
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId());
        lqw.eq(Dish::getStatus,1);
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(lqw);
        return R.success(list);
    }
}
